<template>
  <div>
    <div class="header-container">
      <div class="l-content">
        <div class="humburger-container" @click="toggleClick">
          <svg-icon :iconClass="icon"/>

        </div>
        <div style="color:#1890ff;cursor: pointer;" @click="gotoHome()">
          <i class="el-icon-s-home" style="font-size: 16px;">首页</i>

        </div>
      </div>
      <div class="r-content">
        <!-- <div style="margin-right: 20px;cursor: pointer;font-size: 20px;" @click="signIn">
          <i class="el-icon-edit"></i>
          <span style="font-size: 16px">签到</span>
        </div> -->
        <div>
          <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          <el-avatar :size="40" :src="circleUrl"></el-avatar>
          <span class="user-name">{{ $store.getters.staff.moniker }}</span>
          <!--            <img class="user" src="../../assets/404_images/404.png" alt="">-->
        </span>
            <el-dropdown-menu slot="dropdown" style="width: 130px;">
              <!-- <el-dropdown-item>个人中心</el-dropdown-item> -->
              <!-- <el-dropdown-item command="file">附件管理</el-dropdown-item> -->
              <!-- <el-dropdown-item command="signOut">签退</el-dropdown-item> -->
              <el-dropdown-item command="logout">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>


    <!-- 附件上传 -->
    <el-dialog class="fileUpdate" title="附件上传" :visible.sync="visible" width="60%" :center="false"
               labelPosition="left"
    >
      <avue-form :option="option" v-model="form">
        <template slot-scope="{disabled,size}" slot="file">
          <div class="block" style="width: 200px;" v-if="!form.apkName">
            <el-button type="primary">

              上传

            </el-button>
            <input class="imgBtn" type="file" accept="image/*" @change="handleFileChange">
          </div>

          <div v-else>
            <div>{{ form.apkName }}</div>
            <div class="uploadBtn">
              <el-button type="primary">
                重新上传
              </el-button>
              <input class="imgBtn" type="file" accept="image/*" @change="handleFileChange" placeholder="">
            </div>

          </div>

        </template>


      </avue-form>
      <div class="operateBtn">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
      </div>


    </el-dialog>
  </div>


</template>
<script>

import { getFile, getFileUpload, getFileAdd, getFileEdit, sign9001, signSave9001, signOut9002 } from '@/api/file'
import router from '@/router'
import { ybCommon } from '@/api/user'
import axios from 'axios'
import panelGroup from '@/views/dashboard/admin/components/PanelGroup'
import { ybPost } from '@/utils/ybRequest'

export default {
  data() {
    return {
      userName: '',
      form: {},
      visible: false,
      circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      option: {
        labelWidth: 120,
        span: 24,
        emptyBtn: false,
        submitBtn: false,
        menuPosition: 'center',
        column: [
          {
            label: '上传文件',
            prop: 'file'
          },
          {
            label: '版本号',
            prop: 'version'
          },
          {
            label: '更新信息',
            prop: 'msg'
          },
          //  {
          //     label:'路径',
          //     prop:'path'
          //  },

          {
            label: '是否强制更新',
            prop: 'isForceUpdate',
            type: 'radio',
            span: 24,
            dicData: [{
              label: '是',
              value: true
            }, {
              label: '否',
              value: false
            }],
            value: false
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            maxlength: 20,
            minRows: 3,
            maxRows: 5

          }
        ]
      },
      signInInfo: {}//医保签到信息
    }
  },
  computed: {
    icon() {
      return this.$store.getters.siderType ? 'hamburger-opened' : 'hamburger-closed'
    }
  },
  mounted() {
    this.userName = this.$store.getters.staff.moniker
  },
  methods: {
    gotoHome() {
      router.push({ path: '/home' })
    },
    toggleClick() {
      this.$store.commit('user/CHANGE_SIDER_TYPE')
    },
    onload() {
      getFile().then((res) => {
        if (res.data.isForceUpdate === null) {
          res.data.isForceUpdate = false
        }
        this.form = res.data

      })
    },
    handleSubmit() {
      let form = this.form
      let data = {
        version: form.version,
        msg: form.msg,
        apkName: form.apkName,
        apkSize: form.apkSize,
        isForceUpdate: form.isForceUpdate,
        remark: form.remark
      }
      if (!form.id) {
        this.addFun(data)
      } else {
        data.id = form.id

        this.editFun(data)
      }
    },
    addFun(data) {
      getFileAdd(data).then((res) => {
        console.log('add', res)
        if (res.success) {
          this.$message({
            type: 'success',
            message: '新增成功!'
          })
          this.visible = false
          this.onload()
        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })
    },
    editFun(data) {
      console.log('修改')

      getFileEdit(data).then((res) => {
        if (res.success) {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.visible = false
          this.onload()
        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })

    },
    handleClose() {
      this.visible = false

    },
    handleMenu() {
      // this.$store.commit('collapseMenu');
    },
    handleCommand(command) {
      console.log(command)
      if (command == 'file') {
        this.onload()
        this.visible = true
      } else if (command == 'logout') {
        this.$store.dispatch(('user/logout'))
      } else if (command == 'signOut') {
        this.signOut()
      }
    },
    handleFileChange(event) {
      const file = event.target.files[0]
      const formData = new FormData()
      formData.append('file', file)

      getFileUpload(formData).then((res) => {
        if (res.success) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.form.apkName = file.name
          this.form.apkSize = file.size

        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })
    },
    signIn() {
      sign9001().then(res => {
        const { astr_jyhb, astr_jysr } = res.data
        ybPost(astr_jysr).then(res => {
          console.log(res)
          this.$store.commit('user/SET_SIGNININFO', res)
          signSave9001(res).then(res => {
            this.$message.success('签到成功!')
          }).catch(err => {
            this.$message.error('签到失败,请联系管理员!')
          })
        }).catch(err => {
          this.$message.error('签到失败,请联系管理员!')
        })
        /*   axios({
            method: 'post',
            url: 'http://10.30.20.27:8080/mbs-fsi/web/api/fsi/callService',
            data: astr_jysr,
            timeout: 30000,
            headers: {
              'Content-Type': 'application/json'
            }
          }).then(res => {
            if (res.data.err_msg == 'success') {
              this.$store.commit('user/SET_SIGNININFO', res.data)
              signSave9001(res.data).then(res => {
                this.$message.success('签到成功!')
              }).catch(err => {
                this.$message.error('签到失败,请联系管理员!')
              })
            }
          }).catch(err => {
            this.$message.error('签到失败,请联系管理员!')
          }) */
      })
    },
    signOut() {
      signOut9002({ signNo: this.$store.getters.signInInfo.output.signinoutb.sign_no }).then(res => {
        console.log(res)
        const { astr_jyhb, astr_jysr } = res.data
        ybPost(astr_jysr).then(res => {
          this.$message.success('签退成功!')
        }).catch(err => {
          this.$message.error('签退失败,请联系管理员!')
        })
        /* axios({
          method: 'post',
          url: 'http://10.30.20.27:8080/mbs-fsi/web/api/fsi/callService',
          data: astr_jysr,
          timeout: 30000,
          headers: {
            'Content-Type': 'application/json'
          }
        }).then(res => {
          console.log(res)
          if (res.data.err_msg == 'success') {
            this.$message.success('签退成功!')
          }
        }).catch(err => {
          this.$message.error('签到失败,请联系管理员!')
        }) */
      })
    }
  }
}

</script>

<style scoped>
.l-content {
  display: flex;
}

.header-container {
  width: 100%;
  padding: 0 0;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.r-content {
  display: flex;
  align-items: center;
}

.text {
  color: #fff;
  font-size: 14px;
  margin-left: 10px;
}

.r-content .user {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.fileUpdate {
  text-align: left !important;
}

.operateBtn {
  display: flex;
  justify-content: center;
}

.imgBtn {
  opacity: 0;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  height: 100%;
  width: 100%
}

.uploadBtn {
  /* width: 100px;
  height: 50px; */
  position: relative;
}

.humburger-container {
  margin-right: 16px;
  box-sizing: border-box;
  cursor: pointer;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
}

.user-name {
  margin: 0 10px;
  font-size: 14px;
  font-weight: bolder;
}
</style>
